/*
 * Copyright 2012-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bstek.demo.urule.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bstek.demo.urule.entity.SysUser;
import com.bstek.demo.urule.mapper.SysUserMapper;
import com.bstek.urule.console.database.model.Group;
import com.bstek.urule.console.database.model.User;
import com.bstek.urule.console.database.service.project.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SsoService {
    @Autowired
    SysUserMapper sysUserMapper;

    public User ssoValidate(String account) {
        LambdaQueryWrapper<SysUser> lambda = new LambdaQueryWrapper<>();
        lambda.eq(SysUser::getUserName, account)
                .last("limit 1");
        SysUser sysUser = sysUserMapper.selectOne(lambda);
        if (sysUser == null) {
            throw new RuntimeException("账号不存在");
        }
        com.bstek.urule.console.database.service.group.GroupService.ins.addGroupUser("sxdt", account);
        ProjectService.ins.addProjectuser(1, account);
        User user = new User();
        user.setId(String.valueOf(sysUser.getUserName()));
        user.setName(sysUser.getUserFullname());
        user.setPassword(sysUser.getPassword());
        user.setEnable(true);
        user.setCreateDate(sysUser.getCreateDate());
        user.setUpdateDate(sysUser.getUpdateDate());
        user.setEmail(sysUser.getEmail());
        user.setCreateUser(sysUser.getCreateBy());
        user.setUpdateUser(sysUser.getUpdateBy());
        List<Group> groups = com.bstek.urule.console.database.manager.group.GroupManager.ins.createQuery().list(user.getId());
        user.setGroups(groups);
        return user;
    }

}
